$(function () {
    var contextPath = getContextPath();
   var url = contextPath+"/ArticleController/getArticle.press";
    $(".divList").show();
    $(".binList").hide();
    $(".draftList").hide();
    $(".searchList").hide();
    var a={tip:"1"};
    $.post(url,{data:"123"},function (result) {
        //alert(result.mappingList[0].aContent.content_summary);
        //alert(result.mappingList.length);
        var noBinhtml ="";
        var Binhtml="";
        var drafthtml="";
        var type ="";
        var count = 1;
        //动态添加全部不在回收站的文章
        for(var i =0;i<result.contentList.length;i++){
            for(var j=0;j<result.mappingList.length;j++){
                if(result.contentList[i].content_id==result.mappingList[j].content_id){
                    type += result.mappingList[j].aTaxonomy.taxonomy_title+" ";
                }
            }
            noBinhtml = noBinhtml + "<tr><td>"+count+"<input type='hidden' class='hidden' value='"+result.contentList[i].content_id+"'/></td>" +
                "<td><a href='#'>"+result.contentList[i].content_title+"</a><br/><div class='div'><a href="+getContextPath()+'/ArticleController/getContentDetail.press?tip=save&&content_id='+result.contentList[i].content_id+" class='edit'>编辑</a>|" +
                "<a href='#' class='moveToBin'>移至垃圾箱</a></div></td>" +
                "<td>"+result.contentList[i].content_author +"</td>" +
                "<td>"+type+"</td>" +
                "<td>"+result.contentList[i].content_created+"</td></tr>"
            count++;
            type="";
        }
        // 动态添加回收站的文章
            type="";
            count=1;
            for(var i =0;i<result.contentBinList.length;i++){
                for(var j=0;j<result.mappingList.length;j++){
                    if(result.contentBinList[i].content_id==result.mappingList[j].content_id){
                        type += result.mappingList[j].aTaxonomy.taxonomy_title+" ";
                    }
                }
                if(type==" "){
                    type="无";
                }
                Binhtml = Binhtml + "<tr><td>"+count+"<input type='hidden' class='hidden' value='"+result.contentBinList[i].content_id+"'/></td>" +
                    "<td><a href='#'>"+result.contentBinList[i].content_title+"</a><br/><div class='binDiv'><a href='#' class='recover'>还原</a>|<a href='#' class='remove'>永久删除</a></div></td>" +
                    "<td>"+result.contentBinList[i].content_author +"</td>" +
                    "<td>"+type+"</td>" +
                    "<td>"+result.contentBinList[i].content_created+"</td></tr>"
                count++;
                type="";
        }
        // 动态添加草稿箱的文章
        type="";
        count=1;
        for(var i =0;i<result.contentDraftList.length;i++){
            for(var j=0;j<result.mappingList.length;j++){
                if(result.contentDraftList[i].content_id==result.mappingList[j].content_id){
                    type += result.mappingList[j].aTaxonomy.taxonomy_title+" ";
                }
            }
            if(type==" "){
                type="无";
            }
            drafthtml = drafthtml + "<tr><td>"+count+"<input type='hidden' class='hidden' value='"+result.contentDraftList[i].content_id+"'/></td>" +
                "<td><a href='#'>"+result.contentDraftList[i].content_title+"</a><br/><div class='draftDiv'><a href="+getContextPath()+'/ArticleController/getContentDetail.press?tip=draft&&content_id='+result.contentDraftList[i].content_id+" class='edit' >编辑</a>|" +
                "<a href='#' class='remove'>永久删除</a></div></td>" +
                "<td>"+result.contentDraftList[i].content_author +"</td>" +
                "<td>"+type+"</td>" +
                "<td>"+result.contentDraftList[i].content_created+"</td></tr>"
            count++;
            type="";
        }
        $(".divList .table tr").after(noBinhtml);
        $(".binList .table tr").after(Binhtml);
        $(".draftList .table tr").after(drafthtml);
        $("#bin").html("回收站("+ result.binCount+")");
        $("#noBin").html("全部("+ result.noBinCount+")");
        $("#draft").html("草稿箱("+ result.draftCount+")");
        $(".div").hide();
        $(".binDiv").hide();
        $(".draftDiv").hide();
        showDiv();
        hideDiv();
        click();
    },"json");
    topClick(a);
    $(".king-btn-demo").click(function () {
      //  alert(123);
       // alert(a.tip);
        var url3 =  getContextPath()+"/ArticleController/search.press";
        var title = $(".search #search").val();
        //var html="";
        var tip="";
        if(a.tip=="1"){
            tip="noBin";
        }else if(a.tip=="2"){
            tip="bin";
        }else if(a.tip=="3"){
            tip="draft";
        }
        //alert("zhjixing ");
        $(".searchList tr:not(:first)").empty();
        $.post(url3,{"content_title":title,
        "tip":tip},function (result) {
            var html="";
            var type="";
            var count=1;
            for(var i =0;i<result.searchList.length;i++){
                for(var j=0;j<result.mappingList.length;j++){
                    if(result.searchList[i].content_id==result.mappingList[j].content_id){
                        type += result.mappingList[j].aTaxonomy.taxonomy_title+" ";
                    }
                }
                if(type==" "){
                    type="无";
                }
                html = html + "<tr><td>"+count+"<input type='hidden' class='hidden' value='"+result.searchList[i].content_id+"'/></td>" +
                    "<td><a href='#'>"+result.searchList[i].content_title+"</a><br/><div class='searchDiv'></div></td>" +
                    "<td>"+result.searchList[i].content_author +"</td>" +
                    "<td>"+type+"</td>" +
                    "<td>"+result.searchList[i].content_created+"</td></tr>"
                count++;
                type="";
            }
            //alert(result.searchList[0].content_tip);
            if("Bin"==result.searchList[0].content_tip){
               // alert("垃圾箱");
                $(".searchList .table tr:last").after(html);
                var divHtml = "<a href='#' class='recover'>还原</a>|<a href='#' class='remove'>永久删除</a>";
                $(".searchDiv").html(divHtml);
            }else if("noBin"==result.searchList[0].content_tip){
               // alert("全部");
                $(".searchList .table tr:last").after(html);
                var divHtml = "<a href="+getContextPath()+'/ArticleController/getContentDetail.press?tip=save&&content_id='+$(this).parent().parent().prev().find(".hidden").attr("value")+" class='edit'>编辑</a>|" +
                    "<a href='#' class='moveToBin'>移至垃圾箱</a>";
                $(".searchDiv").html(divHtml);
            }else if("draft"==result.searchList[0].content_tip){
               // alert("草稿箱");
                $(".searchList .table tr:last").after(html);
                var divHtml = "<a href="+getContextPath()+'/ArticleController/getContentDetail.press?tip=draft&&content_id='+$(this).parent().parent().prev().find(".hidden").attr("value")+" class='edit' >编辑</a>|" +
                    "<a href='#' class='remove'>永久删除</a>";
                $(".searchDiv").html(divHtml);
            }
            $(".searchDiv").hide();
            showDiv();
            hideDiv();
            click();
        },"json");
        $(".divList").hide();
        $(".binList").hide();
        $(".draftList").hide();
        $(".searchList").show();
    });
});
    function showDiv() {
        $(".divList .table tr").mouseover(function () {
            // var html = "";
            // $(this).children()[0].after(html);
            $(this).find(".div").show();
            $(this).css('font-weight','bolder');
        });
        $(".binList .table tr").mouseover(function () {
            $(this).find(".binDiv").show();
            $(this).css('font-weight','bolder');
        });
        $(".draftList .table tr").mouseover(function () {
            $(this).find(".draftDiv").show();
            $(this).css('font-weight','bolder');
        });
        $(".searchList .table tr").mouseover(function () {
            $(this).find(".searchDiv").show();
            $(this).css('font-weight','bolder');
        });
    }
    function hideDiv() {
        $(".divList .table tr").mouseleave(function () {
            $(this).find(".div").hide();
            $(this).css('font-weight','normal');
        });
        $(".binList .table tr").mouseleave(function () {
            $(this).find(".binDiv").hide();
            $(this).css('font-weight','normal');
        });
        $(".draftList .table tr").mouseleave(function () {
            $(this).find(".draftDiv").hide();
            $(this).css('font-weight','normal');
        });
        $(".searchList .table tr").mouseleave(function () {
            $(this).find(".searchDiv").hide();
            $(this).css('font-weight','normal');
        });
    }
    function click() {
        $(".moveToBin").click(function () {
            var contentId = $(this).parent().parent().prev().find(".hidden").attr("value");
            var url2 = getContextPath()+"/ArticleController/moveToBin.press";
            $.post(url2, {
                "content_id": contentId,
                "tip": "Bin"
            }, function (result) {
                if (result.msg == "yes") {
                    alert("移除成功！")
                    window.location.reload();
                } else {
                    alert("系统错误！移除失败！");
                }
            }, "json");
        });
        $(".recover").click(function () {
            var contentId = $(this).parent().parent().prev().find(".hidden").attr("value");
            var url2 = getContextPath()+"/ArticleController/moveToBin.press";
            $.post(url2, {
                    "content_id": contentId,
                    "tip": "NoBin"
                },
                function (result) {
                    if (result.msg == "yes") {
                        alert("还原成功！")
                        window.location.reload();
                    } else {
                        alert("系统错误！移除失败！");
                    }
                }, "json")
        });
        $(".remove").click(function () {
            var contentId = $(this).parent().parent().prev().find(".hidden").attr("value");
            var url2 = getContextPath()+"/ArticleController/delete.press";
            if (confirm("确定要永久删除吗？")) {
                $.post(url2, {"content_id": contentId},
                    function (result) {
                        if (result.msg == "yes") {
                            alert("删除成功！")
                            window.location.reload();
                        } else {
                            alert("系统错误！删除失败！");
                        }
                    }, "json")
            }
        });
    }
    function topClick(a) {
     $("#total").click(function () {
        $(".divList").show();
        $(".binList").hide();
        $(".draftList").hide();
        $(".searchList").hide();
        a.tip="1";
    });
     $("#recyleBin").click(function () {
        $(".divList").hide();
        $(".binList").show();
        $(".draftList").hide();
        $(".searchList").hide();
        a.tip="2";
    });
     $("#draf").click(function () {
        $(".divList").hide();
        $(".binList").hide();
        $(".draftList").show();
        $(".searchList").hide();
        a.tip="3";
    });
}